package com.jushu.storbox.newfragment;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

import android.app.ActionBar.LayoutParams;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Message;

import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.jushu.storbox.FileBrowserAllFileActivity;
import com.jushu.storbox.FileBrowserPictureActivity;
import com.jushu.storbox.MyFileSearchActivity;
import com.jushu.storbox.R;
import com.jushu.storbox.ShareDriveApplication;
import com.jushu.storbox.TargetFolderBrowserActivity;
import com.jushu.storbox.TeamFolderBrowserActivity;
import com.jushu.storbox.actions.FileAction;
import com.jushu.storbox.actions.FolderAction;
import com.jushu.storbox.actions.NodeAction;
import com.jushu.storbox.actions.UploadAction;
import com.jushu.storbox.constant.ClientConfig;
import com.jushu.storbox.constant.Constant;
import com.jushu.storbox.constant.IntentConstant;
import com.jushu.storbox.constant.MessageCode;
import com.jushu.storbox.constant.UiConstant;
import com.jushu.storbox.entities.FileFolderInfo;
import com.jushu.storbox.entities.TempObjectCache;
import com.jushu.storbox.handler.ClientExceptionRelateHandler;
import com.jushu.storbox.newfragment.argument.FileActionArgs;
import com.jushu.storbox.util.LogUtil;
import com.jushu.storbox.util.PublicTools;
import com.jushu.storbox.view.dialog.ClouddriveDialog;
import com.jushu.storbox.view.dialog.CreateFolderDialog;
import com.jushu.storbox.view.dialog.PopupMenuWindow;
import com.jushu.storbox.view.resolve.FilleDisplayResolve;
import com.huawei.sharedrive.sdk.android.modelV2.response.FolderResponse;
import com.kennyc.view.MultiStateView;

public class FileFragment extends AbsFrameFragment {

	protected static final String TAG = FileFragment.class.getSimpleName();

	TitleViewHolder titleHolder = null;
	// PullToRefreshListView lsContentList = null;

	PopupWindow titleMore = null;
	View titleMoreView = null;
	View createFolder = null;
	View mutilSelect = null;
	View sortByName = null;
	View sortByTime = null;
	View sortBySize = null;

	FrameLayout multiFooterContainer = null;
	View multiFooterView = null;
	FrameLayout multiTitleContainer = null;
	View multiTitleView = null;

	FilleDisplayResolve fileListResolve;
	boolean MultiSelectMode = false;

	private ProgressDialog waitDialog;
	private ClouddriveDialog addFileDialog;

	public boolean multiSelectFlag;

	@Override
	protected int getTitleResourceId() {
		return R.layout.action_title_style_one;
	}

	@Override
	protected int getContentResourceId() {
		return R.layout.action_body_style_one;
	}

	protected void startInitConpentData() {
		waitDialog = new ProgressDialog(getMainFrameActivity());
		waitDialog.setMessage(getMainFrameActivity().getResources().getString(R.string.loading));
	}

	protected void onLoadTitleView(View contentView) {
		titleHolder = new TitleViewHolder(contentView);
	}

	protected void onLoadContentView(View contentView) {
		fileListResolve = new FilleDisplayResolve(getMainFrameActivity(), getArgumentBundle(), (MultiStateView) contentView.findViewById(R.id.multiStateView)) {
			@Override
			public void onMove(int index, FileFolderInfo info) {
				Intent intent = new Intent(context, TargetFolderBrowserActivity.class);
				Bundle bundle = new Bundle();
				bundle.putString(IntentConstant.INTENT_TARGET_RESOURCE_OWNER, info.getOwnerId());
				bundle.putInt(IntentConstant.INTENT_SOURCE_RESOURCE_TYPE, info.getType());
				bundle.putInt(IntentConstant.INTENT_COMEFROM_REQUEST, IntentConstant.INTENT_REQUEST_MOVEFILE);//
				bundle.putSerializable(IntentConstant.INTENT_DATA_EXTENDS_1, info);
				intent.putExtra(IntentConstant.INTENT_SEARCH_TARGET_DATA, bundle);
				startActivityForResult(intent, IntentConstant.INTENT_REQUEST_SEARCH_TARGET);
			}

			@Override
			public void onTransferToTeam(int index, FileFolderInfo info) {
				Intent intent = new Intent(context, TeamFolderBrowserActivity.class);
				Bundle bundle = new Bundle();
				bundle.putInt(IntentConstant.INTENT_SOURCE_RESOURCE_TYPE, info.getType());
				bundle.putSerializable(IntentConstant.INTENT_DATA_EXTENDS_1, info);
				intent.putExtra(IntentConstant.INTENT_SEARCH_TEAMSPACE_DATA, bundle);
				startActivityForResult(intent, IntentConstant.INTENT_REQUEST_COPY_TEAM_TARGET);
			}
			
			@Override
			protected void updateCurrentFolder(String ownerId, String folderId, String folderName) {
				if (Constant.ROOT_FOLDER_ID.equals(folderId)) {
					titleHolder.ivReturn.setVisibility(View.GONE);
					titleHolder.tvTitle.setText(R.string.myfiles);
				} else {
					titleHolder.ivReturn.setVisibility(View.VISIBLE);
					titleHolder.tvTitle.setText(folderName);
				}
			}
		
			
		};
		fileListResolve.getFolderStack().clear();
		fileListResolve.initComponent((PullToRefreshListView) contentView.findViewById(R.id.file_action_content_list));
		fileListResolve.binddingAdapter();
	}

	@Override
	protected void onInflaterOtherView(LayoutInflater inflater, ViewGroup container) {
		super.onInflaterOtherView(inflater, container);
		titleMoreView = inflater.inflate(R.layout.adapter_view_title_more_item, container, false);
		createFolder = titleMoreView.findViewById(R.id.adapter_view_title_menu_createFolder);
		mutilSelect = titleMoreView.findViewById(R.id.adapter_view_title_menu_mutilSelect);
		sortByName = titleMoreView.findViewById(R.id.adapter_view_title_menu_sortByName);
		sortByTime = titleMoreView.findViewById(R.id.adapter_view_title_menu_sortByTime);
		sortBySize = titleMoreView.findViewById(R.id.adapter_view_title_menu_sortBySize);
		titleMore = new PopupWindow(titleMoreView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {
		case MessageCode.UPLOAD_SELECT_FILE_REQCODE:
			if (resultCode == MessageCode.UPLOAD_SELECT_FILE_RESULTCODE) {
				LogUtil.i(TAG, "Get the Uri of the selected file.");
				Bundle bundle = data.getBundleExtra("result");

				List<String> resultUrlList = bundle.getStringArrayList("resultUIL");
				new UploadAction().uploadFile(getMainFrameActivity(), ShareDriveApplication.getInstance().getWnerID(), fileListResolve.getFolderArgument()
						.getFolderId(), resultUrlList, msgHandler);
			}
			break;
		case IntentConstant.INTENT_REQUEST_SEARCH_TARGET:
			switch (resultCode) {
			case UiConstant.NODE_ACTION_MOVE_SUCCESS: // 单个文件|文件夹移动成功
				Bundle bundle = data.getBundleExtra(IntentConstant.INTENT_SEARCH_TARGET_DATA);
				FileFolderInfo info = (FileFolderInfo) bundle.getSerializable(IntentConstant.INTENT_DATA_EXTENDS_1);
				fileListResolve.getAdapter().removeData(info);
				Toast.makeText(getActivity(), R.string.action_file_promit_move_source_success, Toast.LENGTH_SHORT).show();
				break;

			case UiConstant.NODE_ACTION_MOVE_CANCEL: // 单个文件|文件夹取消自动重命名
				break;
			}
			break;
		case IntentConstant.INTENT_REQUEST_SEARCH_MOVE_TARGET:

			Bundle bundle = data.getBundleExtra(IntentConstant.INTENT_SEARCH_TARGET_DATA);
			ArrayList<FileFolderInfo> successList = (ArrayList<FileFolderInfo>) bundle.getSerializable(IntentConstant.INTENT_DATA_EXTENDS_1);
			switch (resultCode) {
			case UiConstant.NODES_ACTION_MOVE_SOME_SUCCESS: // 批量移动部分成功
				for (FileFolderInfo info : successList)
					fileListResolve.getAdapter().removeData(info);
				// Toast.makeText(getActivity(),
				// R.string.action_file_promit_move_some_source_success,
				// Toast.LENGTH_SHORT).show();
				new TitleViewHolder().cancelMultiSelect();
				break;

			case UiConstant.NODES_ACTION_MOVE_CANCAL: // 批量移动取消
				for (FileFolderInfo info : successList) {
					fileListResolve.getAdapter().removeData(info);
				}
				// Toast.makeText(getActivity(),
				// R.string.action_file_promit_move_source_success_except_duplication_of_name,
				// Toast.LENGTH_SHORT).show();
				new TitleViewHolder().cancelMultiSelect();
				break;

			case UiConstant.NODES_ACTION_MOVE_ALL_SUCCESS: // 批量移动所有成功
				for (FileFolderInfo info : successList) {
					fileListResolve.getAdapter().removeData(info);
				}
				Toast.makeText(getActivity(), R.string.action_file_promit_move_source_success, Toast.LENGTH_SHORT).show();
				new TitleViewHolder().cancelMultiSelect();
				break;
			}
			break;

		case IntentConstant.INTENT_REQUEST_COPY_TEAM_TARGET:
			switch (resultCode) {
			case MessageCode.TRANSFER_TO_MY_TEAM_SUCCESS: // 单个文件（夹）转存到团队空间成功
				Toast.makeText(getActivity(), R.string.operate_success, Toast.LENGTH_SHORT).show();
				break;
			case UiConstant.SHARE_ACTION_SAVE_CANCEL: // 单个文件（夹）转存到团队空间,取消自动重命名
				// Toast.makeText(getActivity(),
				// R.string.action_file_copy_tema_cancel,
				// Toast.LENGTH_SHORT).show();
				break;
			}
			break;

		case IntentConstant.INTENT_REQUEST_FIND_TEAM_SPACE:
			switch (resultCode) {
			case UiConstant.NODES_ACTION_COPY_ALL_SUCCESS: // 批量转存到团队空间所有成功
				Toast.makeText(getActivity(), R.string.operate_success, Toast.LENGTH_SHORT).show();
				new TitleViewHolder().cancelMultiSelect();
				break;
			case UiConstant.NODES_ACTION_COPY_TEMA_CANCEL: // 批量转存到团队空间，取消自动重命名
				// Toast.makeText(getActivity(),
				// R.string.action_file_copy_tema_cancel,
				// Toast.LENGTH_SHORT).show();
				new TitleViewHolder().cancelMultiSelect();
				break;
			case UiConstant.NODES_ACTION_COPY_SOME_SUCCESS: // 部分成功
				break;
			}
			break;
			
		case IntentConstant.INTENT_REQUEST_SEARCHFILE:
			switch (resultCode){
			case IntentConstant.INTENT_RESULT_SEARCHFILE_NULL:
				break;
			case IntentConstant.INTENT_RESULT_SEARCHFILE_VALUE:
				 FileFolderInfo info = (FileFolderInfo) data.getSerializableExtra(IntentConstant.INTENT_SEARCH_FILE_DATA);
				 new NodeAction().getNodePath(getActivity(), msgHandler, info);
				 break;
			}
			break;
		}

		super.onActivityResult(requestCode, resultCode, data);
	}

	protected void onBackPressed() {
		LogUtil.e(TAG, "use default backpress listener");
		if(multiSelectFlag){
			new TitleViewHolder().cancelMultiSelect();
		}else{
			fileListResolve.openParentFolder();
		}
	}

	protected boolean touchRoot() {
		if (fileListResolve.getFolderStack().size() <= 1) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public FileActionArgs getArgumentBundle() {
		FileActionArgs tempArgument = (FileActionArgs) super.getArgumentBundle();
		if (tempArgument == null) {
			tempArgument = new FileActionArgs();
			tempArgument.setFolderId(Constant.ROOT_FOLDER_ID);
			tempArgument.setFolderName(getActivity().getResources().getString(R.string.my_cloud_drive));
			setArgument(tempArgument);
		}
		return tempArgument;
	}

	public class TitleViewHolder {
		View titleContainer;
		View rlReturn;
		View ivReturn;
		TextView tvTitle;
		View imgSearch;
		View imgUpload;
		View imgMore;

		public TitleViewHolder() {
		}

		public TitleViewHolder(View container) {
			titleContainer = container;
			ivReturn = titleContainer.findViewById(R.id.file_action_title_returnIcon);
			tvTitle = (TextView) titleContainer.findViewById(R.id.file_action_title_folderId);
			rlReturn = titleContainer.findViewById(R.id.file_action_title_return_Rl);
			rlReturn.setOnClickListener(new OnClickListener() {
				public void onClick(View arg0) {
					if (!handleBackPressed()) {
						LogUtil.i(TAG, "no more parent folder!");
					}
				}
			});
			imgSearch = titleContainer.findViewById(R.id.file_action_title_search);
			imgSearch.setOnClickListener(new OnClickListener() {
				public void onClick(View arg0) {
					Intent intent = new Intent(getMainFrameActivity(), MyFileSearchActivity.class);
					String folderName = getResources().getString(R.string.my_cloud_drive);
					String ownerId = ShareDriveApplication.getInstance().getWnerID();
					intent.putExtra(IntentConstant.INTENT_DATA_EXTENDS_1, ownerId);
					intent.putExtra(IntentConstant.INTENT_DATA_EXTENDS_2, folderName);
					startActivityForResult(intent, IntentConstant.INTENT_REQUEST_SEARCHFILE);
				}
			});
			imgUpload = titleContainer.findViewById(R.id.file_action_title_upload);
			imgUpload.setOnClickListener(new OnClickListener() {
				public void onClick(View arg0) {
					addFileDialog = new ClouddriveDialog(getActivity(), R.style.dialog_upload, R.layout.dialog_addfile);
					addFileDialog.setPosition(Gravity.CENTER_HORIZONTAL, Gravity.CENTER_VERTICAL);
					WindowManager windowManager = getActivity().getWindowManager();
					Display display = windowManager.getDefaultDisplay();
					addFileDialog.setWidth(display.getWidth());
					addFileDialog.show();
					dialogAddfileClick();
				}
			});
			imgMore = titleContainer.findViewById(R.id.file_action_title_moreOperation);
			imgMore.setOnClickListener(new OnClickListener() {
				public void onClick(View view) {
					PopupMenuWindow popMenuWindow = new PopupMenuWindow(getActivity(), (int) getActivity().getResources().getDimension(
							R.dimen.item_popup_more_window_width_l), LayoutParams.WRAP_CONTENT, R.layout.adapter_view_title_more_item) {
						public void onInflaterFinished(View contentView) {
							View createFolder = contentView.findViewById(R.id.adapter_view_title_menu_createFolder);
							createFolder.setOnClickListener(new OnClickListener() {
								public void onClick(View arg0) {
									dismiss();
									new CreateFolderDialog().createFolder(getMainFrameActivity(), ShareDriveApplication.getInstance().getWnerID(),
											fileListResolve.getFolderArgument().getFolderId(), msgHandler, waitDialog);
								}
							});
							View mutilSelect = contentView.findViewById(R.id.adapter_view_title_menu_mutilSelect);
							mutilSelect.setOnClickListener(new OnClickListener() {
								public void onClick(View arg0) {
									dismiss();
									multiSelectAction();
								}
							});
							View sortByName = contentView.findViewById(R.id.adapter_view_title_menu_sortByName);
							sortByName.setOnClickListener(new OnClickListener() {
								public void onClick(View arg0) {
									dismiss();
									fileListResolve.sortByName(getSortType("name"));
								}
							});
							View sortByTime = contentView.findViewById(R.id.adapter_view_title_menu_sortByTime);
							sortByTime.setOnClickListener(new OnClickListener() {
								public void onClick(View arg0) {
									dismiss();
									fileListResolve.sortByTime(getSortType("time"));
								}
							});
							View sortBySize = contentView.findViewById(R.id.adapter_view_title_menu_sortBySize);
							sortBySize.setOnClickListener(new OnClickListener() {
								public void onClick(View v) {
									dismiss();
									fileListResolve.sortBySize(getSortType("size"));
								}
							});
						}
					};
					int[] location = new int[2];
					view.getLocationOnScreen(location);

					// -5 -- background border is 5dp
					popMenuWindow.showAtLocation(titleContainer, Gravity.NO_GRAVITY, location[0] - popMenuWindow.getWidth() + view.getWidth(), location[1]
							+ titleContainer.getHeight() - PublicTools.dipTopx(getActivity(), 5));
				}
			});
		}

		private int getSortType(String type) {
			SharedPreferences preference = getActivity().getSharedPreferences(ClientConfig.settings, Context.MODE_APPEND);
			if (type.equals("size"))
				return (preference.getInt(UiConstant.FILE_SORT_SIZE, UiConstant.SORT_BY_SIZE) ^ 0x1000);
			else if (type.equals("time"))
				return (preference.getInt(UiConstant.FILE_SORT_TIME, UiConstant.SORT_BY_TIME) ^ 0x1000);
			else
				// (type.equals("NAME"))
				return (preference.getInt(UiConstant.FILE_SORT_NAME, UiConstant.SORT_BY_NAME) ^ 0x1000);
		}

		private void multiSelectAction() {
			if (multiFooterContainer == null) {
				multiFooterContainer = (FrameLayout) getMainFrameActivity().findViewById(R.id.main_frame_footer_multisel);
				multiFooterView = LayoutInflater.from(getMainFrameActivity()).inflate(R.layout.action_multisel_bottom_items, multiFooterContainer, false);
				multiFooterContainer.addView(multiFooterView);

				multiTitleContainer = (FrameLayout) getMainFrameActivity().findViewById(R.id.main_frame_title_fl_multi);
				multiTitleView = LayoutInflater.from(getMainFrameActivity()).inflate(R.layout.action_title_multisel, multiTitleContainer, false);
				ViewGroup.LayoutParams layoutParam = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
				multiTitleContainer.addView(multiTitleView, layoutParam);

				TextView cancelView = (TextView) multiTitleView.findViewById(R.id.multi_title_cancel);
				cancelView.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View arg0) {
						cancelMultiSelect();
					}
				});
				final TextView selectAllView = (TextView) multiTitleView.findViewById(R.id.multi_title_selectall);
				selectAllView.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View arg0) {
						if (selectAllView.getText().toString().equals(getString(R.string.select_all_translate_items))) {
							fileListResolve.getAdapter().selectAll();
							selectAllView.setText(getString(R.string.cancel_select_all));
						} else {
							fileListResolve.getAdapter().cancelSelectAll();
							selectAllView.setText(getString(R.string.select_all_translate_items));
						}
					}
				});

				View teamspaceView = (View) multiFooterView.findViewById(R.id.multi_select_teamspace);
				teamspaceView.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View arg0) {
						ArrayList<FileFolderInfo> datas = (ArrayList<FileFolderInfo>) fileListResolve.getAdapter().getSelectedItems();
						if (datas == null || datas.isEmpty()) {
							Toast.makeText(getActivity(), R.string.action_move_no_item_been_selected, Toast.LENGTH_SHORT).show();
							return;
						}
						TempObjectCache.instance().putObjectList(IntentConstant.INTENT_SEARCH_TARGET_DATA, datas);

						Intent intent = new Intent(getActivity(), TeamFolderBrowserActivity.class);
						Bundle bundle = new Bundle();
						bundle.putSerializable("datas", datas);
						System.out.println(datas.size() + ">>>>" + TAG);
						intent.putExtra(IntentConstant.INTENT_SEARCH_TEAMSPACE_DATA, bundle);
						startActivityForResult(intent, IntentConstant.INTENT_REQUEST_FIND_TEAM_SPACE);
					}
				});
				View copyView = (View) multiFooterView.findViewById(R.id.multi_select_copy);
				copyView.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View arg0) {
						ArrayList<FileFolderInfo> datas = (ArrayList) fileListResolve.getAdapter().getSelectedItems();
						if (datas == null || datas.isEmpty()) {
							Toast.makeText(getActivity(), R.string.action_move_no_item_been_selected, Toast.LENGTH_SHORT).show();
							return;
						}
						TempObjectCache.instance().putObjectList(IntentConstant.INTENT_SEARCH_TARGET_DATA, datas);
						String ownerId = ShareDriveApplication.getInstance().getWnerID();
						Intent intent = new Intent(getActivity(), TargetFolderBrowserActivity.class);

						Bundle bundle = new Bundle();
						bundle.putSerializable(UiConstant.NODE_MOVE_DATAS, datas);
						bundle.putString(IntentConstant.INTENT_TARGET_RESOURCE_OWNER, ownerId);
						bundle.putInt(IntentConstant.INTENT_COMEFROM_REQUEST, IntentConstant.INTENT_REQUEST_MOVEFILE);//
						intent.putExtra(IntentConstant.INTENT_SEARCH_TARGET_DATA, bundle);
						startActivityForResult(intent, IntentConstant.INTENT_REQUEST_SEARCH_MOVE_TARGET);

					}
				});
				View deleteView = (View) multiFooterView.findViewById(R.id.multi_select_delete);
				deleteView.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View arg0) {
						final List<FileFolderInfo> datas = fileListResolve.getAdapter().getSelectedItems();
						if (datas == null || datas.isEmpty()) {
							Toast.makeText(getActivity(), R.string.action_move_no_item_been_selected, Toast.LENGTH_SHORT).show();
							return;
						}

						final ClouddriveDialog delete = new ClouddriveDialog(getActivity(), R.style.dialog_upload, R.layout.dialog_filedelete);
						View conventView = delete.getConventView();
						WindowManager windowManager=getActivity().getWindowManager();
						Display display=windowManager.getDefaultDisplay();
						delete.setWidth(display.getWidth());
						delete.setPosition(Gravity.CENTER_HORIZONTAL, Gravity.CENTER_VERTICAL);
						Button dialog_cancal = (Button) conventView.findViewById(R.id.dialog_add_file_cancal_bt);

						Button dialog_OK = (Button) conventView.findViewById(R.id.dialog_add_file_ok_bt);

						TextView dialog_prompt_tv = (TextView) conventView.findViewById(R.id.dialog_prompt_tv);
						dialog_prompt_tv.setText(getActivity().getString(R.string.allfile_confirm_delete_filefolder));

						dialog_cancal.setOnClickListener(new View.OnClickListener() {
							@Override
							public void onClick(View v) {
								delete.dismiss();
								cancelMultiSelect();
							}
						});

						dialog_OK.setOnClickListener(new View.OnClickListener() {
							@Override
							public void onClick(View v) {
								delete.dismiss();
								new NodeAction().deleteNodes(getActivity(), msgHandler, UiConstant.NODE_ACTION_DELETE_SUCCESS, datas);
								cancelMultiSelect();
							}
						});
						delete.show();
						// new NodeAction().deleteNodes(getActivity(),
						// msgHandler,
						// UiConstant.NODE_ACTION_DELETE_SUCCESS, datas);
					}
				});
				View downloadView = (View) multiFooterView.findViewById(R.id.multi_select_download);
				downloadView.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View arg0) {
						List<FileFolderInfo> datas = fileListResolve.getAdapter().getSelectedItems();
						if (datas == null || datas.isEmpty()) {
							Toast.makeText(getActivity(), R.string.action_move_no_item_been_selected, Toast.LENGTH_SHORT).show();
							return;
						}
						new NodeAction().downloadNodes(getActivity(), msgHandler, UiConstant.NODE_ACTION_DOWNLOAD_SOME_SUCCESS, datas, true);
						int cont = datas.size();
						cancelMultiSelect();
						LogUtil.d(TAG, "the size of list is: " + cont);
					}
				});
			}
			getMainFrameActivity().hiddenFootContainer();
			getMainFrameActivity().hiddenTitleContainer();
			multiFooterContainer.setVisibility(View.VISIBLE);
			multiTitleContainer.setVisibility(View.VISIBLE);
			TextView selectAllView = (TextView) multiTitleView.findViewById(R.id.multi_title_selectall);
			selectAllView.setText(getString(R.string.select_all_translate_items));
			fileListResolve.enableMultiSelect();
			multiSelectFlag = true;
		}

		public void cancelMultiSelect() {
			getMainFrameActivity().displayFootContainer();
			getMainFrameActivity().displayTitleContainer();
			multiFooterContainer.setVisibility(View.GONE);
			multiTitleContainer.setVisibility(View.GONE);
			fileListResolve.disableMultiSelect();
			multiSelectFlag  = false;
		}
	}

	public void dialogAddfileClick() {
		View conventView = addFileDialog.getConventView();
		Button dialog_add_file = (Button) conventView.findViewById(R.id.dialog_add_file);

		Button dialog_add_photo = (Button) conventView.findViewById(R.id.dialog_add_photo);

		Button dialog_addfile_cancel = (Button) conventView.findViewById(R.id.dialog_addfile_cancel);

		dialog_add_file.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent dataAllFile = new Intent();
				dataAllFile.setClass(getMainFrameActivity(), FileBrowserAllFileActivity.class);
				startActivityForResult(dataAllFile, MessageCode.UPLOAD_SELECT_FILE_REQCODE);
				if (null != addFileDialog) {
					addFileDialog.dismiss();
				}
			}
		});
		dialog_add_photo.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent dataPicture = new Intent();
				dataPicture.setClass(getActivity(), FileBrowserPictureActivity.class);
				startActivityForResult(dataPicture, MessageCode.UPLOAD_SELECT_FILE_REQCODE);
				if (null != addFileDialog) {
					addFileDialog.dismiss();
				}
			}
		});
		dialog_addfile_cancel.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (null != addFileDialog) {
					addFileDialog.dismiss();
				}
			}
		});
	}

	public void doUpdateCurrentFolder(String ownerId, String folderId, String folderName) {
//		if (Constant.ROOT_FOLDER_ID.equals(folderId)) {
//			titleHolder.ivReturn.setVisibility(View.GONE);
//			titleHolder.tvTitle.setText(R.string.myfiles);
//		} else {
//			titleHolder.ivReturn.setVisibility(View.VISIBLE);
//			titleHolder.tvTitle.setText(folderName);
//		}
		LogUtil.e("doUpdateCurrentFolder", "NO Use agine...");
	}

	ClientExceptionRelateHandler msgHandler = new ClientExceptionRelateHandler() {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MessageCode.UPLOAD_FAIL:
				Toast.makeText(getMainFrameActivity(), getMainFrameActivity().getResources().getString(R.string.fileListadapter_upload_failed),
						Toast.LENGTH_SHORT).show();
				break;
			case MessageCode.UPLOAD_SUCCESS:
				FileFolderInfo fileInfo = (FileFolderInfo) msg.obj;
				Toast.makeText(getMainFrameActivity(),
						"[" + fileInfo.getName().toString() + "]" + getMainFrameActivity().getResources().getString(R.string.fileListadapter_upload_success),
						Toast.LENGTH_SHORT).show();
				if (!fileListResolve.getAdapter().contains(fileInfo)) {
					fileListResolve.addData(0, fileInfo);
					// fileListResolve.addData(fileInfo);
				}
				break;
			case MessageCode.CREATE_FOLDER_SUCCESS:
				waitDialog.dismiss();
				fileListResolve.addData(0, (FileFolderInfo) msg.obj);
				// fileListResolve.addData((FileFolderInfo) msg.obj);
				break;
			case UiConstant.NODE_ACTION_MOVE_SUCCESS:
				FileFolderInfo info = (FileFolderInfo) msg.obj;
				fileListResolve.getAdapter().removeData(info);
				showToast(R.string.action_file_promit_move_source_success, Toast.LENGTH_SHORT);
				break;
			case UiConstant.NODE_ACTION_DELETE_SUCCESS:
				FileFolderInfo deleteInfo = (FileFolderInfo) msg.obj;
				fileListResolve.getAdapter().removeData(deleteInfo);
				break;
			case UiConstant.NODE_ACTION_GET_PATH:
				Bundle bundle = msg.getData();
				FileFolderInfo folderinfo = (FileFolderInfo) bundle.getSerializable(IntentConstant.INTENT_DATA_EXTENDS_1);
				List<FolderResponse> values = (List<FolderResponse>) msg.obj;
				Stack<FileActionArgs> fileStack = fileListResolve.getFolderStack();
				fileStack.clear();
				FileActionArgs root = new FileActionArgs();
				root.setFolderId(Constant.ROOT_FOLDER_ID);
				root.setFolderName(getString(R.string.my_cloud_drive));
				root.setOwnerId(folderinfo.getOwnerId());
				fileStack.push(root);
				for(int i=values.size()-1; i>=0; i--){
					FolderResponse value = values.get(i);
					FileActionArgs object = new FileActionArgs();
					object.setFolderId(value.getId());
					object.setFolderName(value.getName());
					object.setOwnerId(value.getOwnerBy());
					fileStack.push(object);
				}
				fileListResolve.openFolderWithWaiting(0, folderinfo);
				break;
				
			default:
				if (waitDialog.isShowing()) {
					waitDialog.dismiss();
				}
				super.handleMessage(msg);
			}

		}

		protected void onException_409_RepeatNameConflict(final Message msg, String errorCode) {
			final Bundle bundle = msg.getData();
			if (bundle.getString("handle") != null) {
				final ClouddriveDialog move = new ClouddriveDialog(getActivity(), R.style.dialog_upload, R.layout.dialog_filedelete);
				View conventView = move.getConventView();
				Button dialog_cancal = (Button) conventView.findViewById(R.id.dialog_add_file_cancal_bt);
				Button dialog_OK = (Button) conventView.findViewById(R.id.dialog_add_file_ok_bt);
				TextView dialog_prompt_tv = (TextView) conventView.findViewById(R.id.dialog_prompt_tv);
				dialog_prompt_tv.setText(getActivity().getString(R.string.allfile_confirm_reanme_filefolder));
				dialog_cancal.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						move.dismiss();
					}
				});
				dialog_OK.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						try {
							move.dismiss();
							if (bundle.getString("type").equals("isFile")) {
								new FileAction().moveFileWithAutoRename(getActivity(), bundle.getInt("messageCode"),
										(FileFolderInfo) bundle.getSerializable("sourceNode"), bundle.getString("targetFolderId"),
										bundle.getString("targetFolderOwnerId"), msgHandler);

							} else if (bundle.getString("type").equals("isFolder")) {
								new FolderAction().moveFolderWithAutoRename(getActivity(), bundle.getInt("messageCode"),
										(FileFolderInfo) bundle.getSerializable("sourceNode"), bundle.getString("targetFolderId"),
										bundle.getString("targetFolderOwnerId"), msgHandler);
							}
						} catch (Error e) {
							e.printStackTrace();
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				});
				move.show();
			} else {
				super.onException_409_RepeatNameConflict(msg, errorCode);
			}

		}

		@Override
		public Context getContext() {
			return getActivity();
		}
	};

	public void updateSelectAll(boolean selectState) {
		TextView selectAllView = (TextView) multiTitleView.findViewById(R.id.multi_title_selectall);
		if (selectState) {
			selectAllView.setText(getString(R.string.cancel_select_all));
		} else {
			selectAllView.setText(getString(R.string.select_all_translate_items));
		}
	}

	public boolean isMultiSelectFlag() {
		return multiSelectFlag;
	}

	public void setMultiSelectFlag(boolean multiSelectFlag) {
		this.multiSelectFlag = multiSelectFlag;
	}

	public boolean isMultiSelectMode() {
		return MultiSelectMode;
	}

	public void setMultiSelectMode(boolean multiSelectMode) {
		MultiSelectMode = multiSelectMode;
	}

}
